<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板(ID)</title>
    <style>
        #box {
            height: 400px;
            width: 500px;
            background-color: pink;
            display: flex;
            flex-direction: column;
            align-items: center;
            border-radius: 20px;
        }

        #message {
            height: 200px;
            width: 440px;
            background-color: white;
            margin-top: 30px;
            margin-bottom: 10px;
            border-radius: 20px;
            padding: 10px;
            font-size: 20px;
        }
        input{
            height: 30px;
            width: 440px;
            margin: 10px 0 10px 0;
            border-radius: 5px;
        }
        #btn{
            height: 30px;
            width: 100px;
            margin-top: 10px;
            border-radius: 5px;
        }
    </style>
</head>

<body>
    <div id="box">
        <div id="message"></div>
        <input type="text" placeholder="请输入昵称" id="name">
        <input type="text" placeholder="请输入留言" id="content">
        <button id="btn">提交</button>
    </div>
    <script>
        let messEle = document.getElementById("message");
        let nameEle = document.getElementById("name");
        let conEle = document.getElementById("content");
        let btnEle = document.getElementById("btn");
        btnEle.addEventListener("click", function () {
            let names = nameEle.value;
            let contents = conEle.value;
            //存入消息对象到本地
            let k = Date.now();
            let messages = {id: k,name: names,content: contents};
            localStorage.setItem("messages", JSON.stringify(messages)); 
            messEle.innerText += names + ":" + contents + "\n";
            nameEle.value = "";
            conEle.value = "";   
        })
    </script>
</body>

</html>